// page init
jQuery(function(){
	initOpenClose();
	initPage();
	initCustomGallery();
});

// open-close init
function initOpenClose() {
	jQuery('div.slide-block').OpenClose({
		activeClass:'active',
		opener:'a.open-close',
		slider:'div.block',
		effect:'slide',
		animSpeed:500
	});
}

// open-close plugin
jQuery.fn.OpenClose = function(_options){
	// default options
	var _options = jQuery.extend({
		activeClass:'active',
		opener:'.opener',
		slider:'.slide',
		animSpeed: 400,
		animStart:false,
		animEnd:false,
		effect:'fade',
		event:'click'
	},_options);

	return this.each(function(){
		// options
		var _holder = jQuery(this);
		var _slideSpeed = _options.animSpeed;
		var _activeClass = _options.activeClass;
		var _opener = jQuery(_options.opener, _holder);
		var _slider = jQuery(_options.slider, _holder);
		var _animStart = _options.animStart;
		var _animEnd = _options.animEnd;
		var _effect = _options.effect;
		var _event = _options.event;
		if(_slider.length) {
			_opener.bind(_event,function(){
				if(!_slider.is(':animated')) {
					if(typeof _animStart === 'function') _animStart();
					if(_holder.hasClass(_activeClass)) {
						_slider[_effect=='fade' ? 'fadeOut' : 'slideUp'](_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
						_holder.removeClass(_activeClass);
					} else {
						_holder.addClass(_activeClass);
						_slider[_effect=='fade' ? 'fadeIn' : 'slideDown'](_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
					}
				}
				return false;
			});
			if(_holder.hasClass(_activeClass)) _slider.show();
			else _slider.hide();
		}
	});
}

function initCustomGallery(){
	var _invisClass = 'invisible';
	var _animSpeed = 750;
	$('.gallery').each(function(){
		var _holder = $(this);
		var _slideEl = $('.slider', _holder);
		var _slides = $('.slide', _slideEl);
		var _btNext = $('.btn-next', _holder);
		var _btPrev = $('.btn-prev', _holder);
		var _active = 0;
		var _bigW, _minW;
		
		_btPrev.hide();
		_slides.each(function(_ind){
			var _slide = $(this);
			var _activeS = $('.active-slide', _slide);
			var _defaultS = $('.default-slide', _slide);
			if (_ind == _active) {
				_activeS.show().removeClass(_invisClass);
				_defaultS.hide().removeClass(_invisClass);
				_bigW = _activeS.width();
			}
			else {
				_activeS.hide().removeClass(_invisClass);
				_defaultS.show().removeClass(_invisClass);
				_minW = _defaultS.width();
			}
		})
		function checkButtons(){
			if (_active == 0) {
				_btPrev.hide();
				_btNext.show();
			}
			else if (_active == _slides.length - 1){
				_btPrev.show();
				_btNext.hide();
			}
			else {
				_btPrev.show();
				_btNext.show();
			}
		}
		
		function changeSlide(from, to){
			_active = to;
			_slideEl.animate({
				'margin-left':-_minW*_active
			}, _animSpeed);
			setTimeout(function(){
				_slides.eq(from).find('.active-slide').hide();
				_slides.eq(from).find('.default-slide').show();
				_slides.eq(to).find('.active-slide').fadeIn(_animSpeed/2);
				_slides.eq(to).find('.default-slide').fadeOut(_animSpeed/2);
			}, _animSpeed/2)
			checkButtons();
		}
		
		_btNext.click(function(){
			if (!_slideEl.is(':animated')) changeSlide(_active, _active+1)
			return false;
		})
		_btPrev.click(function(){
			if (!_slideEl.is(':animated')) changeSlide(_active, _active-1)
			return false;
		})
	})
}

function initPage()
{
	initNav({
		menuId: "main-nav",
		cleverMode: true,
		hoverClass: "hover",
		flexibility: true,
		sideClasses: true,
		menuPaddings: 40,
		minWidth: 100
	});

}
function initNav(o)
{
	if (!o.menuId) o.menuId = "main-nav";
	if (!o.cleverMode) o.cleverMode = false;
	if (!o.flexibility) o.flexibility = false;
	if (!o.dropExistenceClass) o.dropExistenceClass = false;
	if (!o.hoverClass) o.hoverClass = "hover";
	if (!o.menuHardCodeClass) o.menuHardCodeClass = "menu-hard-code";
	if (!o.sideClasses) o.sideClasses = false;
	if (!o.center) o.center = false;
	if (!o.menuPaddings) o.menuPaddings = 0;
	if (!o.minWidth) o.minWidth = 0;
	if (!o.coeff) o.coeff = 1.7;
	var n = document.getElementById(o.menuId);
	if(n)
	{
		n.className = n.className.replace(o.menuHardCodeClass, "");
		var lfl = [];
		var li = n.getElementsByTagName("li");
		for (var i=0; i<li.length; i++)
		{
			li[i].className += (" " + o.hoverClass);
			var d = li[i].getElementsByTagName("div").item(0);
			if(d)
			{
				if(o.flexibility)
				{
					var a = d.getElementsByTagName("a");
					for (var j=0; j<a.length; j++)
					{
						var w = a[j].parentNode.parentNode.offsetWidth;
						if(w > 0)
						{
							if(typeof(o.minWidth) == "number" && w < o.minWidth)
								w = o.minWidth;
							else if(typeof(o.minWidth) == "string" && li[i].parentNode == n && w < li[i].offsetWidth)
								w = li[i].offsetWidth - 3;
							a[j].style.width = w - o.menuPaddings + "px";
						}
					}
					d.style.width = li[i].getElementsByTagName("div").item(1).clientWidth + "px";
				}
				var t = document.documentElement.clientWidth/o.coeff;
				if(li[i].parentNode != n && (!o.cleverMode || fPX(li[i]) < t))
				{
					d.style.right = "auto";
					d.style.left = li[i].parentNode.offsetWidth + "px";
					d.parentNode.className += " left-side";
				}	
				else if(li[i].parentNode != n && (o.cleverMode || fPX(li[i]) >= t))
				{
					d.style.left = "auto";
					d.style.right = li[i].parentNode.offsetWidth + "px";
					d.parentNode.className += " right-side";
				}
				else if(li[i].parentNode == n && o.cleverMode && fPX(li[i]) >= t)
				{
					li[i].className += " right-side";
				}
				if(li[i].parentNode == n && o.center)
					d.style.left = -li[i].getElementsByTagName("div").item(1).clientWidth/2 + li[i].clientWidth/2 + "px";
			}
			if(o.dropExistenceClass && li[i].getElementsByTagName("ul").length > 0)
			{
				li[i].className += (" " + o.dropExistenceClass);
				li[i].getElementsByTagName("a").item(0).className += (" " + o.dropExistenceClass + "-link");
				li[i].innerHTML += "<em class='pointer'></em>";
			}
			if(li[i].parentNode == n) lfl.push(li[i]);
		}
		if(o.sideClasses)
		{
			lfl[0].className += " first-child";
			lfl[0].getElementsByTagName("a").item(0).className += " first-child-link";
			lfl[lfl.length-1].className += " last-child";
			lfl[lfl.length-1].getElementsByTagName("a").item(0).className += " last-child-link";
		}
		for (var i=0; i<li.length; i++)
		{
			li[i].className = li[i].className.replace(o.hoverClass, "");
			li[i].onmouseover = function()
			{
				this.className += (" " + o.hoverClass);
			}
			li[i].onmouseout = function()
			{
				this.className = this.className.replace(o.hoverClass, "");
			}
		}
	}
	function fPX(a)
	{
		var b = 0;
		while (a.offsetParent) {b += a.offsetLeft; a = a.offsetParent;}
		return b;
	}
}
